//
//  GainMidCell.swift
//  DSP
//
//  Created by tucici on 2024/5/17.
//

import UIKit
import TCCKit
let updateAllMute = "updateAllMute";
class GainMidCell: UITableViewCell {
    @IBOutlet var vol: UIButton!
    @IBOutlet var dB: UILabel!
    @IBOutlet var sub: UIButton!
    @IBOutlet var add: UIButton!
    @IBOutlet var mute: UIButton!
    private var slider : DSlider!
    override func awakeFromNib() {
        super.awakeFromNib()
        initUI()
    }
    
    func initUI(){
        backgroundColor = .clear
        
        slider = DSlider()
        slider.delegate = self
        slider.maxValue = 0
        slider.minValue = -50
        slider.currentValue = 0
        slider.thumImg.image = UIImage(named: "slider_p")
        slider.setColors([HEX_RED.color(), HEX_RED.color()])
       
        contentView.addSubview(slider)
        slider.snp.makeConstraints { make in
            make.height.equalTo(30.k_h)
            make.left.equalTo(sub.snp.right).offset(5.k_w)
            make.right.equalTo(add.snp.left).offset(-5.k_w)
            make.centerY.equalToSuperview()
        }
        updateUI()
    }
    
    func updateUI(){
        dB.text = "\(Int(APP.CHM.volume))dB"
        slider.currentValue = CGFloat(APP.CHM.volume)
        
        //sub.isEnabled = APP.CHM.enable
        //add.isEnabled = APP.CHM.enable
       //mute.isEnabled = APP.CHM.enable
        //slider.enable = APP.CHM.enable
        
        /*mute.isSelected = APP.CHM.mute
        mute.bgHexColor = !APP.CHM.enable ? HEX_BG_DISABLE : mute.isSelected ? HEX_RED : HEX_BG
        mute.borderHexColor = !APP.CHM.enable ? HEX_BORDER_DISABLE : mute.isSelected ? HEX_RED : HEX_BLACK
        mute.textHexColor = !APP.CHM.enable ? HEX_TEXT_DISABLE : HEX_BLACK
        
        dB.textHexColor = !APP.CHM.enable ? HEX_TEXT_DISABLE : HEX_BLACK
        dB.borderHexColor = !APP.CHM.enable ? HEX_BORDER_DISABLE : HEX_BLACK
         */
        
        mute.isSelected = APP.CHM.mute
        mute.bgHexColor = mute.isSelected ? HEX_RED : HEX_BG
        mute.borderHexColor = mute.isSelected ? HEX_RED : HEX_BLACK
       // mute.textHexColor = !APP.CHM.enable ? HEX_TEXT_DISABLE : HEX_BLACK
        
        //dB.textHexColor = !APP.CHM.enable ? HEX_TEXT_DISABLE : HEX_BLACK
        //dB.borderHexColor = !APP.CHM.enable ? HEX_BORDER_DISABLE : HEX_BLACK
    }
    
    func sendData(){
        _ = BLEData.allMute(APP.CHM.mute, Float(APP.CHM.volume)).write()
    }
    
    
    @IBAction func actionForBtn(_ sender: UIButton) {
        switch sender{
        case vol:
            return
        case sub:
            if APP.CHM.volume <= -50 {return}
            APP.CHM.volume -= 1
        case add:
            if APP.CHM.volume >= 0 {return}
            APP.CHM.volume += 1
        case mute:
            APP.CHM.mute = !APP.CHM.mute
        default:
            break
        }
        updateUI()
        sendData()
        NotifyTool.post(updateAllMute)
    }
}

//MARK: - DSliderDelgate
extension GainMidCell : DSliderDelgate{
    func valueForChange(_ sender: DSlider) {
        APP.CHM.volume = Int(sender.currentValue)
        updateUI()
        sendData()
        NotifyTool.post(updateAllMute)
    }
    
    func valueForChangeEnd(_ sender: DSlider) {
        sendData()
    }
}

